package com.ketangpai.admin.mapper;

import com.ketangpai.admin.entity.UserCourse;
import com.ketangpai.admin.entity.dto.UserCourseDTO;
import com.ketangpai.admin.entity.param.CountParam;
import com.ketangpai.admin.entity.param.UserCourseParam;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserCourseMapper {

    @Select("select * from user_course")
    List<UserCourse> selectList();

    /**
        查询用户置顶课程
     */
    @Select("select * from user_course where user_id=#{userId} and is_top = 0 and is_pigeonhole =1 and ( CASE WHEN #{type} = 1 THEN type != 1 ELSE type !=-1 END)")
    List<UserCourse> selectTopClass(Integer userId, Integer type);
    /**
     查询用户学习课程
     */
    @Select("select * from user_course where user_id=#{userId} and type=0  and is_pigeonhole =1")
    List<UserCourse> selectLearnClass(Integer userId);
    /**
     查询用户教学课程
     */
    @Select("select * from user_course where user_id=#{userId} and type=1 and is_pigeonhole =1")
    List<UserCourse> selectTeachClass(Integer userId);
    /**
     查询用户协助课程
     */
    @Select("select * from user_course where user_id=#{userId} and type=2 and is_pigeonhole =1")
    List<UserCourse> selectAssistClass(Integer userId);
    /**
     查询用户归档学习课程
     */
    @Select("select * from user_course where user_id=#{userId} and is_pigeonhole = 0 and type =0")
    List<UserCourse> selectPigeonholeStudyClass(Integer userId);
    /**
     查询用户归档教学课程
     */
    @Select("select * from user_course where user_id=#{userId} and is_pigeonhole = 0 and type =1")
    List<UserCourse> selectPigeonholeTeachClass(Integer userId);
    /**
     查询用户协助课程
     */
    @Select("select * from user_course where user_id=#{userId} and is_pigeonhole = 0 and type =2")
    List<UserCourse> selectPigeonholeAssistClass(Integer userId);
    /**
     *取消置顶
     */
    @Update("update user_course set is_top = 1  where user_id = #{userId} and course_id = #{courseId}")
    boolean unTopById(UserCourseParam courseParam);
    /**
     *通过用户id和课程id和用户类型查询课程
     */
    @Select("SELECT * from user_course  where user_id = #{userId} and course_id = #{courseId} and type = #{type}")
    UserCourseParam isAlreadyAdd(UserCourseParam courseParam);
    /**
     *通过用户id和课程id删除课程
     */
    @Delete("delete from user_course where user_id = #{userId} and course_id = #{courseId}")
    boolean deleteById(UserCourseParam userCourseParam);
    /**
     * 添加课程
     */
    @Insert("insert into user_course(user_id,course_id,type,is_top,is_pigeonhole,enter_time) values(#{userId},#{courseId},0,1,1,now())")
    boolean addClass(UserCourseParam courseParam);
    /**
     *通过用户id和课程id查询课程学年
     */
    @Select("select academic_year,semester ,count(*) from course group by academic_year,semester order by academic_year")
    boolean selectAcademicYearAndSemester();
    /**
     *通过用户id和课程id查询课程
     */
    @Select("select * from user_course where user_id = #{userId} and course_id = #{courseId}")
    UserCourseDTO getUser(UserCourseParam param);
    /**
     *通过用户id和课程id置顶课程
     */
    @Update("update user_course set is_top = 0  where user_id = #{userId} and course_id = #{courseId}")
    boolean topById(UserCourseParam courseParam);

    /**
     *归档课程
     */
    @Update("update user_course set is_pigeonhole = 0  where user_id = #{userId} and course_id = #{courseId}")
    boolean pigeonholeClass(UserCourseParam courseParam);
    /**
     *取消归档课程
     */
    @Update("update user_course set is_pigeonhole = 1  where user_id = #{userId} and course_id = #{courseId}")
    boolean unPigeonholeClass(UserCourseParam courseParam);
    /**
     查询用户全部课程
     */
    @Select("select * from user_course where user_id=#{userId}")
    List<UserCourse> selectAllClass(Integer userId);

    /**
     * 创建课程
     */
    @Insert("insert into user_course(user_id,course_id,type,is_top,is_pigeonhole,enter_time) values(#{userId},#{courseId},1,1,1,now())")
    boolean createClass(UserCourseParam courseParam);

    /**
     *通过用户id和课程id删除课程
     */
    @Delete("delete from user_course where  course_id = #{courseId}")
    boolean deleteTeachById(UserCourseParam userCourseParam);

    /**
     *更新学生数量
     */
    @Select("select count(*) as count , course_id as courseId from user_course group by course_id")
    List<CountParam> getUserCount();

    /**
     *通过用户id和课程id查询学习课程
     */
    @Select("select * from user_course where course_id=#{courseId} and type=0")
    List<UserCourse> getStudent(UserCourseParam courseParam);

    /**
     *新增助教
     */
    @Insert("insert into user_course(user_id,course_id,type,is_top,is_pigeonhole,enter_time) values(#{userId},#{courseId},#{type},1,1,now())")
    boolean insertAssist(UserCourse userCourse);

    /**
     *通过用户id和课程id查询课程
     */
    @Select("select * from user_course where course_id=#{courseId} and type= !0 and user_id=#{userId}")
    List<UserCourse> selectById(UserCourse userCourse);
}
